package com.runningboy.es_demo.controller;

import com.runningboy.es_demo.dto.DocDto;
import com.runningboy.es_demo.service.ESService;
import jdk.nashorn.internal.runtime.regexp.joni.SearchAlgorithm;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 用于实现ES增删查改功能
 * @Author Runningboy
 * @Date 2020/6/24 20:14
 * @Version 1.0
 */
@RestController
public class ESController {

    @Autowired
    private ESService esService;

    @GetMapping("/index")
    public String index(){
        return null;
    }

    /**
     * 简单查询
     * @param id
     * @return
     */
    @GetMapping("/get/man/girl/{id}")
    public ResponseEntity get(@PathVariable String id){
        GetResponse response = esService.getDocById(id);
        return new ResponseEntity(response.getSource(), HttpStatus.OK);
    }

    /**
     * 新增一条数据
     * @param docDto
     * @return
     */
    @PostMapping("/add/man/girl")
    public ResponseEntity add(@RequestBody DocDto docDto){
        return esService.addDoc(docDto);
    }

    /**
     * 删除一条数据
     * @param id
     * @return
     */
    @DeleteMapping("/del/man/girl/{id}")
    public ResponseEntity del(@PathVariable("id") String id){
        return esService.del(id);
    }

    /**
     * 修改一条数据
     * @param id
     * @param docDto
     * @return
     */
    @PutMapping("/update/man/girl/{id}")
    public ResponseEntity update(@PathVariable String id,@RequestBody DocDto docDto){
        return esService.update(id,docDto);
    }


    /**
     * 合查询，例如：查询国籍是中国或者china的，同时查询年龄段20-30的
     * @param country
     * @param ltAge
     * @param gtAge
     * @return
     */
    @PostMapping("/query/man/girl")
    public String query(@RequestParam String country,@RequestParam Integer gtAge,@RequestParam Integer ltAge){
        return esService.query(country, gtAge, ltAge).toString();
    }
}
